Recap of Functions
What We’ve Covered So Far
“Calling” or using a function:
1 |
functionName(); |
“Passing” or giving information to a function:
1 |
functionName(parameter); |
“Passing” or giving multiple pieces of information to a function:
1 |
functionName(parameter1, parameter2, parameter3); |
“Returning” or getting information out of a function:
You need to return data into a variable. You also need to make sure that the variable type matches the type of data you are returning from the function.
1 2 |
int returnValue; returnValue = functionName(parameter); |
“Defining” or writing a function:
You always need a function header and curly brackets to contain the statements inside your function. The parts of the function header are return variable type, function name, parentheses to contain the passed variable types and name, the passed variable(s) type, the passed variable name(s) and if there is more than one passed variable you will also need a comma between the passed variables.
1 2 3 4 |
type name (variableType1 parameter1, variableType2 parameter2, ...) { //statements } |
Questions and Activities to Try-
1. Try putting the code that you created to write your name in the array lesson into a function. That should be pretty easy, the tough part is making the function header. It will have a return type of void and no parameters.
2. Now try making a function that returns a character. Any single character you like. Then print that character out onto the LCD like you did in the character lesson. Remember, you’re going to need to create a character type variable as well as creating a function.